package org.findbugs.report.entity;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Node;

public class Bug {

    private String type;
    private Integer priority;
    private String category;
    private String clazz;
    private String method;
    private String field;
    private boolean isInField;
    private List<Integer> sourceLines;

    public static List<Bug> getBugs(Document findbugsDocument) {
        List<Bug> bugs = new LinkedList<Bug>();
        List<Node> bugNodesList = findbugsDocument.selectNodes("/BugCollection/BugInstance");
        Iterator<Node> it = bugNodesList.iterator();
        while(it.hasNext()) {
            Node bugNode = it.next();
            Bug bug = new Bug();
            bug.setType(bugNode.selectSingleNode("@type").getText());
            bug.setCategory(bugNode.selectSingleNode("@category").getText());
            bug.setPriority(Integer.valueOf(bugNode.selectSingleNode("@priority").getText()));
            bug.setClazz(bugNode.selectSingleNode("Class/@classname").getText());
            if(bugNode.selectSingleNode("Method") != null) {
                bug.setMethod(bugNode.selectSingleNode("Method/@name").getText());
                bug.setInField(false);
            } else if(bugNode.selectSingleNode("Field") != null) {
                bug.setField(bugNode.selectSingleNode("Field/@name").getText());
                bug.setInField(true);
            }
            List<Integer> sourceLines = new LinkedList<Integer>();
            List<Node> sourceLinesNodes = bugNode.selectNodes("SourceLines");
            Iterator<Node> itSourceLines = sourceLinesNodes.iterator();
            while(itSourceLines.hasNext()) {
                Node sourceLineNode = it.next();
                sourceLines.add(Integer.valueOf(sourceLineNode.selectSingleNode("@start").getText()));
            }
            bug.setSourceLines(sourceLines);
            bugs.add(bug);
        }
        return bugs;
    }
    
    public String getType() {
        return type;
    }

    public Integer getPriority() {
        return priority;
    }

    public String getCategory() {
        return category;
    }

    public String getClazz() {
        return clazz;
    }

    public String getMethod() {
        return method;
    }

    public List<Integer> getSourceLines() {
        return sourceLines;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setSourceLines(List<Integer> sourceLines) {
        this.sourceLines = sourceLines;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public boolean isInField() {
        return isInField;
    }

    public void setInField(boolean isInField) {
        this.isInField = isInField;
    }

}
